package cn.kow.kmall.controller.operate;

import cn.kow.kmall.common.constant.RedisKeyConstants;
import cn.kow.kmall.controller.BaseController;
import cn.kow.kmall.core.response.KmallResult;
import cn.kow.kmall.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

/**
 * @Author: lijun
 * @Description:
 * @Date: Created in 2020-04-28 16:35
 * @Modified By:
 */
@Slf4j
@RestController
@RequestMapping("/operate")
public class OperateController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping(value = "/hot-word/list")
    public KmallResult hotWordList() {
        Set<ZSetOperations.TypedTuple> hotSet = redisTemplate.opsForZSet().rangeWithScores(RedisKeyConstants.HOT_WORD, 0, -1);
        return KmallResult.success(hotSet);
    }

    @PostMapping(value = "/hot-word/save")
    public KmallResult hotWordSave(@RequestBody Map<String, String> paramMap) {
        String value = paramMap.get("value");
        Double score = Double.valueOf(paramMap.get("score"));
        redisTemplate.opsForZSet().add(RedisKeyConstants.HOT_WORD, value, Double.valueOf(score));
        return KmallResult.success();
    }

    @PostMapping("/hot-word/del")
    public KmallResult remove(@RequestBody String[] values) {
        redisTemplate.opsForZSet().remove(RedisKeyConstants.HOT_WORD, values);
        return KmallResult.success();
    }
}
